package com.kunlun.basedata.organize.service.impl;

import com.kunlun.basedata.organize.dao.IApplicationDao;
import com.kunlun.basedata.organize.model.ApplicationModel;
import com.kunlun.basedata.organize.model.vo.ApplicationVo;
import com.kunlun.basedata.organize.service.IApplicationService;
import com.kunlun.basedata.utils.CommonUtil;
import com.kunlun.common.model.Page;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 部门业务Service类
 */
@Service
@Transactional
public class ApplicationService implements IApplicationService {

    private final Logger logger = LogManager.getLogger();

    @Autowired
    private IApplicationDao applicationDao;

    @Override
    public List<ApplicationVo> getApplicationList(ApplicationModel ApplicationModel) throws Exception {
        Map<String, Object> queryMap = CommonUtil.packageQueryMap(ApplicationModel, 0, 0);
        return applicationDao.getApplicationList(queryMap);
    }

    @Override
    public Page<ApplicationModel> getApplicationPage(ApplicationModel ApplicationModel, int currentPage, int pageSize) throws Exception {
        int startIndex = (currentPage - 1) * pageSize;
        Map<String, Object> queryMap = CommonUtil.packageQueryMap(ApplicationModel, startIndex, pageSize);
        int count = applicationDao.getApplicationCount(queryMap);
        List<ApplicationModel> allApplication = applicationDao.getApplicationPage(queryMap);

        Page<ApplicationModel> page = new Page<>();
        page.setCurrentPage(currentPage);
        page.setPageSize(pageSize);
        page.setTotal(count);
        page.setRecords(allApplication);
        return page;
    }

    @Override
    public void addApplication(ApplicationModel ApplicationModel) throws Exception {
        ApplicationModel.setId(CommonUtil.generateUUID());
        applicationDao.addApplication(ApplicationModel);
    }

    @Override
    public void updateApplication(ApplicationModel ApplicationModel) throws Exception {
        applicationDao.updateApplication(ApplicationModel);
    }

    @Override
    public void deleteApplication(List<String> list) throws Exception {
        applicationDao.deleteApplication(list);
    }
}
